package com.samphanie.auiu.biz_detail.biz.goods

import android.text.SpannableString
import android.text.Spanned
import android.text.style.AbsoluteSizeSpan
import android.widget.ImageView
import android.widget.TextView
import com.samphanie.auiu.biz_detail.R
import com.samphanie.auiu.common.ui.view.loadUrl
import com.samphanie.auiu.pub_mod.model.SliderImage
import com.samphanie.auiu.ui.banner.AuiuBanner
import com.samphanie.auiu.ui.banner.core.AuiuBannerMo
import com.samphanie.auiu.ui.banner.indicator.AuiuNumIndicator
import com.samphanie.auiu.ui.item.AuiuDataItem
import com.samphanie.auiu.ui.item.AuiuViewHolder

/**
 * @author ZSY
 */
class GoodsDetailHeaderItem(
    val sliderImages: List<SliderImage>?,
    val price: String,
    val completedNumText: String?,
    val goodsName: String?
) : AuiuDataItem<GoodsDetailViewModel, AuiuViewHolder>() {

    override fun onBindData(holder: AuiuViewHolder, position: Int) {
        val context = holder.itemView.context

        val bannerItems = arrayListOf<AuiuBannerMo>()
        sliderImages?.forEach {
            val bannerMo = object : AuiuBannerMo() {}
            bannerMo.url = it.url
            bannerItems.add(bannerMo)
        }

        val auiuBanner = holder.findViewById<AuiuBanner>(R.id.auiu_banner)
        auiuBanner!!.setAuiuIndicator(AuiuNumIndicator(context))
        auiuBanner.setBannerData(bannerItems)
        auiuBanner.setBindAdapter { viewHolder, mo, position ->
            val imageView = viewHolder?.rootView as? ImageView
            mo.let { imageView?.loadUrl(mo!!.url) }
        }

        holder.findViewById<TextView>(R.id.price)?.text = spanPrice(price)
        holder.findViewById<TextView>(R.id.sale_desc)?.text = completedNumText
        holder.findViewById<TextView>(R.id.title)?.text = goodsName

//        binding.auiuBanner.setAuiuIndicator(AuiuNumIndicator(context))
//        binding.auiuBanner.setBannerData(bannerItems)
//        binding.auiuBanner.setBindAdapter { viewHolder, mo, position ->
//            val imageView = viewHolder?.rootView as? ImageView
//            mo.let { imageView?.loadUrl(mo!!.url) }
//        }
//
//        binding.price.text = spanPrice(price)
//        binding.saleDesc.text = completedNumText
//        binding.title.text = goodsName
    }

    private fun spanPrice(price: String?): CharSequence {
        if (price.isNullOrBlank()) return ""

        val ss = SpannableString(price)
        ss.setSpan(AbsoluteSizeSpan(18, true), 1, ss.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        return ss
    }

    override fun getItemLayoutRes(): Int {
        return R.layout.layout_goods_detail_item_header
    }

}